<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Navigation {

	var $CI;
	function __construct()
	{
		$this->CI =& get_instance();
		$this->CI->load->helper(array('url', 'permission'));
		$this->CI->load->library('session');
		$this->CI->load->model('Auth_model');
	}

	/**
	 * create_navigation
	 *
	 * Creates navigation bar for logged user
	 *
	 * @access public
	 * @return string
	 */
	function create_navigation()
	{
		$a_nav = array(
			'Employee',
                        'Setup',
                        'Inventory',
			'Transactions',
			'Reports',
		);

		// gets logged user
		$user_id = $this->CI->session->userdata('user_id');

		// puts html code in array
		$menu = array();
		foreach ($a_nav as $i => $value)
		{
			$sub_menu = $this->sub_menu($value);
			if (count($sub_menu) > 0)
			{
				if ($html = $this->create_html($value, $sub_menu, $user_id))
				{
					$menu[] = $html;
				}
			}
			else
			{
				$details = $this->no_sub($value);
				if ($html = $this->create_html($value, $details, $user_id, NULL, TRUE))
				{
					$menu[] = $html;
				}
			}
		}

		return $menu;
	}

	/**
	 * no_sub
	 *
	 * Sets details for each main menu without subs
	 *
	 * @access public
	 * @param string
	 * @return array
	 */
	function no_sub($main)
	{
		if ($main == 'Inventory')
		{
			return array(
				'identifier' => 'inventory/list_records/',
				'dom_id' => 'nav_inventory',
				'url' => 'inventory',
			);
		}

	}

	/**
	 * sub_menu
	 *
	 * Populates sub menu for each main menu
	 *
	 * @access public
	 * @param string
	 * @return array
	 */
	function sub_menu($main)
	{
		$nav = array();

		if ($main == 'Employee')
		{

                    $nav[] = array(
				'title' => 'User Privilege',
				'url' => 'user_privilege',
				'dom_id' => 'nav_userprivilege',
				'identifier' => 'user_privilege/list_records/',
			);

			$nav[] = array(
				'title' => 'Employees',
				'url' => 'user',
				'dom_id' => 'nav_employee',
				'identifier' => 'user/list_records/',
			);

		}

		if ($main == 'Setup')
		{
			$nav[] = array(
				'title' => 'Item Management',
				'identifier' => 'item/list_records/',
				'dom_id' => 'nav_item',
				'url' => 'item',
			);

			$nav[] = array(
				'title' => 'Warehouse Management',
				'identifier' => 'warehouse/list_records/',
				'dom_id' => 'nav_warehouse',
				'url' => 'warehouse',
			);

		}

		if ($main == 'Transactions')
		{
			$nav[] = array(
				'title' => 'Purchase Order',
				'identifier' => 'purchaseorder/list_records/',
				'dom_id' => 'nav_purchase_order',
				'url' => 'purchaseorder',
			);

			$nav[] = array(
				'title' => 'Receiving Report',
				'identifier' => 'receivingreport/list_records/',
				'dom_id' => 'nav_receiving_report',
				'url' => 'receivingreport',
			);

			$nav[] = array(
				'title' => 'Request Slip',
				'identifier' => 'request_slip/list_records/',
				'dom_id' => 'nav_request_slip',
				'url' => 'request_slip',
			);

			$nav[] = array(
				'title' => 'Gate pass',
				'identifier' => 'gatepass/list_records/',
				'dom_id' => 'nav_gate_pass',
				'url' => 'gatepass',
			);

		}

		if ($main == 'Reports')
		{
			$nav[] = array(
				'title' => 'Item Master List Report',
				'url' => 'imreport',
				'dom_id' => 'nav_item_list',
				'identifier' => 'imreport',
			);

			$nav[] = array(
				'title' => 'Inventory History Report',
				'url' => 'ihreport',
				'dom_id' => 'nav_inventory_report',
				'identifier' => 'ihreport',
			);

                        $nav[] = array(
				'title' => 'Purchase Order Report',
				'url' => 'poreport',
				'dom_id' => 'nav_po_report',
				'identifier' => 'poreport',
			);

                        $nav[] = array(
				'title' => 'Receiving Report (Report)',
				'url' => 'rrreport',
				'dom_id' => 'nav_rr_report',
				'identifier' => 'rrreport',
			);

			$nav[] = array(
				'title' => 'Gate Pass Report',
				'url' => 'gpreport',
				'dom_id' => 'nav_gpreport',
				'identifier' => 'gpreport',
			);

		}

			
		return $nav;
	}

	/**
	 * create_html
	 *
	 * Creates html list code for navigation (suckertree menu)
	 *
	 * @access public
	 * @param string
	 * @param array
	 * @param integer
	 * @param string
	 * @param boolean
	 * @return string
	 */
	function create_html($title, $nav, $user_id, $url = NULL, $no_sub = FALSE)
	{
		$code = '';

		if ($no_sub)
		{
			$where = 'u.user_id = '. $user_id .'
				AND url = "'. $nav['url'] .'"';

			if ($this->CI->Auth_model->has_permissions($where) || $this->CI->session->userdata('super') == 'Y')
			{
				$code .= '<a href="'. site_url() .'/'. $nav['identifier'] .'" id="'. $nav['dom_id'].'">'. $title .'</a>';
			}

			return $code;
		}

		if ( ! empty($url))
		{
			$where = array(
				'u.user_id' => $user_id,
				'url' => $url,
			);

			if ( ! $this->CI->Auth_model->has_permissions($where) && $this->CI->session->userdata('super') == 'N')
			{
				return '';
			}
		}

		$first_code = '<a href="#">'. $title .'</a><ul>';

		$mid_code = '';
		foreach ($nav as $menu)
		{
			$mid_code .= '<li>';

			if (is_array($menu['identifier']))
			{
				$sub_code = $this->create_html($menu['title'], $menu['identifier'], $user_id, $menu['url']);
				if (empty($sub_code) && $mid_code == '')
				{
					$mid_code = '';
					return $mid_code;
				}
				$mid_code .= $sub_code;
			}
			else
			{
				$where = 'u.user_id = '. $user_id .'
					AND url = "'. $menu['url'] .'"';

				if ($this->CI->Auth_model->has_permissions($where) || $this->CI->session->userdata('super') == 'Y')
				{
					if ($menu['url'] !== '#quota')
					{
						$mid_code .= '<a href="'. site_url() .'/'. $menu['identifier'] .'" id="'. $menu['dom_id'].'">'. $menu['title'] .'</a>';

						// Sub-menus For Commission Generate and Commission Record
						if ($menu['url'] == 'commission/generate' || $menu['url'] == 'commission')
						{
							$mid_code .= '<ul style="width: 300px;">
											<li class="border-top">
												<a href="'. site_url() .'/'. $menu['identifier'] .'/salesperson" id="'. $menu['dom_id'].'_sp">Sales Person</a>
											</li>
											<li>
												<a href="'. site_url() .'/'. $menu['identifier'] .'/interiordesigner" id="'. $menu['dom_id'].'_id">Interior Designer</a>
											</li>
										</ul>';
						}
					}
					else
					{	// For Quota Menu under Commission
						$mid_code .= '<a href="#quota" id="'. $menu['dom_id'].'">'. $menu['title'] .'</a>';
					}
				}
				else
				{
					$mid_code = substr($mid_code, 0, -4);
					continue;
				}
			}

			$mid_code .= '</li>';
		}

		if ($mid_code) $code = $first_code . $mid_code .'</ul>';

		return $code;
	}
}
?>
